package org.androidpn.activity;

import org.androidpn.R;
import org.androidpn.client.Constants;
import org.androidpn.client.ServiceManager;
import org.androidpn.http.HttpApi;
import org.androidpn.listener.CommentResponseListener;
import org.androidpn.utils.AppCache;
import org.androidpn.utils.UIHandler;
import org.androidpn.utils.UIHandler.IHandlerListener;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.os.Looper;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;

public abstract class BaseActivity extends FragmentActivity {
	// 消息加载框
	private ProgressDialog mProgressDialog;
	// 提示框
	private AlertDialog.Builder mDialogBuilder;
	// 消息处理
	protected UIHandler mHandler = new UIHandler(Looper.getMainLooper());
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// start the service
		ServiceManager serviceManager = new ServiceManager(this);
		serviceManager.setNotificationIcon(R.drawable.notification);
		serviceManager.startService();
		if(!TextUtils.isEmpty(AppCache.getInstance().getUseInfo().getTelephone())){
			SharedPreferences sharedPrefs = getSharedPreferences(Constants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
			String deviceid = sharedPrefs.getString(Constants.XMPP_USERNAME, "");
			// 注册deviceid
			CommentResponseListener listener = new CommentResponseListener();
			HttpApi.registerDevice(AppCache.getInstance().getUseInfo().getTelephone(), deviceid, listener, listener);
		}
		mHandler.setHanderListener(new IHandlerListener() {

			@Override
			public void handleMessage(Message msg) {
				onHandleMessage(msg);
			}
		});
	}

	protected void sendEmptydMessage(int what) {
		mHandler.sendEmptyMessage(what);
	}

	protected void sendEmptydMessage(int what, int time) {
		mHandler.sendEmptyMessageDelayed(what, time);
	}

	protected void sendMessage(Message msg) {
		mHandler.sendMessage(msg);
	}

	protected void sendMessage(Message msg, int time) {
		mHandler.sendMessageDelayed(msg, time);
	}

	protected void showNoticeDialog(String title, String msg, final OnNoticeDialogListener onNoticeDialogListener) {
		mDialogBuilder = new AlertDialog.Builder(this);
		mDialogBuilder.setTitle(title);
		mDialogBuilder.setMessage(msg);
		mDialogBuilder.setPositiveButton("确定", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				onNoticeDialogListener.onSure();
			}
		});
		mDialogBuilder.setNegativeButton("取消", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				onNoticeDialogListener.onCancel();
			}
		});
		mDialogBuilder.create().show();
	}

	protected void showProgressDialog(String msg) {
		if (mProgressDialog != null && mProgressDialog.isShowing()) {
			mProgressDialog.dismiss();
		}
		mProgressDialog = null;
		try {
			mProgressDialog = ProgressDialog.show(this, "", msg, true, true);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	protected void hideProgressDialog() {
		if (mProgressDialog != null && mProgressDialog.isShowing()) {
			mProgressDialog.dismiss();
		}
		mProgressDialog = null;
	}

	/**
	 * 消息处理
	 * 
	 * @param msg
	 */
	protected abstract void onHandleMessage(Message msg);

	protected interface OnNoticeDialogListener {
		/**
		 * 确定
		 */
		void onSure();

		/**
		 * 取消
		 */
		void onCancel();
	}
}
